using System;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmProductOperation : Form {
        private CategoryBLL _categoryBll = CategoryBLL.GetInstance();
        private ProductBLL _productBll = ProductBLL.GetInstance();

        public FrmProductOperation() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmProductOperation_Load(object sender, EventArgs e) {
            InitDGV();
        }

        private void InitDGV() {
            UpdateProductDGV();
            UpdateCategoryDGV();
        }

        private void UpdateCategoryDGV() {
            dgvCategoryInfo.DataSource = _categoryBll.GetAllCategory();
        }

        private void UpdateProductDGV() {
            dgvProductInfo.DataSource = _productBll.GetAllProduct();
        }

        private void FrmProductOperation_FormClosing(object sender, FormClosingEventArgs e) {
            FrmMain.ObjFrmProductOperation = null;
        }

        private void btnShowAllProductInfo_Click(object sender, EventArgs e) {
            UpdateProductDGV();
        }

        //新增分类
        private void btnAddCategory_Click(object sender, EventArgs e) {
            FrmCategoryUpdate ObjFrmCategoryAdd = new FrmCategoryUpdate();
            ObjFrmCategoryAdd.Text = "Add Category";
            DialogResult dialogResult = ObjFrmCategoryAdd.ShowDialog();
            if (DialogResult.OK == dialogResult) {
                UpdateCategoryDGV();
            }
        }

        private void FrmProductOperation_Activated(object sender, EventArgs e) {
        }

        //修改分类
        private event Action<Category> DataTrans;

        private void btnUpdateCategory_Click(object sender, EventArgs e) {
            FrmCategoryUpdate ObjFrmCategoryUpdate = new FrmCategoryUpdate();
            DataTrans += ObjFrmCategoryUpdate.Init_category;
            DataGridViewRow dataGridViewRow = dgvCategoryInfo.CurrentRow;
            DataTrans.Invoke(new Category() {
                CatId = int.Parse(dataGridViewRow.Cells[0].Value.ToString()),
                CatName = dataGridViewRow.Cells[1].Value.ToString(),
                Remark = dataGridViewRow.Cells[2].Value.ToString(),
                SubTime = Convert.ToDateTime(dataGridViewRow.Cells[4].Value)
            });
            ObjFrmCategoryUpdate.Text = "Update Category";
            DialogResult dialogResult = ObjFrmCategoryUpdate.ShowDialog();

            if (dialogResult == DialogResult.OK) {
                UpdateCategoryDGV();
            }
        }

        //逻辑删除
        private void benDeleteCategory_Click(object sender, EventArgs e) {
            Result<Category> result = _categoryBll.DeleteCategory(new Category()
                { CatId = int.Parse(dgvCategoryInfo.CurrentRow.Cells[0].Value.ToString()) });
            MessageBox.Show(result.Message);
            if (result.IsSuccess) {
                UpdateCategoryDGV();
            }
        }

        private void btnAddPro_Click(object sender, EventArgs e) {
            
        }

        private void txtSearch_TextChanged(object sender, EventArgs e) {
            dgvProductInfo.DataSource = _productBll.GetProductByName(new Product(){ProName = txtSearch.Text});
        }

        private void dgvCategoryInfo_Click(object sender, EventArgs e) {
            dgvProductInfo.DataSource = _productBll.GetProductByCatID(new Product() {
                CatId = int.Parse(dgvCategoryInfo.CurrentRow.Cells[0].Value.ToString())
            });
        }
    }
}